//打包p'packages下的模块
import minimist from "minimist";
import { createRequire } from "module";
import { dirname, resolve } from "path";
import { fileURLToPath } from 'url'
import esbuild from "esbuild";

const require = createRequire(import.meta.url)
const args = minimist(process.argv.slice(2));
const target = args._[0] || "reactivity"; //打包的目标模块
const formart = args.f || "iife"; //打包的模块格式

// esm没有__dirname和__filename
const __fileName = fileURLToPath(import.meta.url)
const __dirName = dirname(__fileName)

const packageDir = resolve(__dirName, "../packages", target);
const entryFile = resolve(packageDir, "src/index.ts");
const pkg = require(resolve(packageDir, "package.json"));

esbuild.context({
    entryPoints: [entryFile],
    outfile: resolve(__dirName, `../packages/${target}/dist/${target}.js`),
    bundle: true,
    platform: "browser",
    sourcemap: true,
    format: formart,
    globalName: pkg.buildOptions?.name,
}).then((ctx) => {
    console.log("build success");
    return ctx.watch();
}).catch(() => {
    console.log("build fail");
})
